//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __SVROBJ_H__
#define __SVROBJ_H__

#include "stub.h"
#include <idmanage.h>
#include <clsinfo.h>
#include <kmarshal.h>
#include <kprxstub.h>

class CProcess;
class Object;

class NameHook : public DLinkNode
{
public:
    Object   * m_pObject;
    CProcess * m_pProcess;   // The process which registerd the name service
    wchar_t    m_wszName[1]; // should be the last variable

    wchar_t   * GetName() { return m_wszName;}

public:
    static DLinkNode  s_hash[CFG_NAME2NAMEHOOK_HS];
    static KMutex     s_nameHookLock;
    static NameHook * Create(Object * pObject,
                                CProcess * pProcess, const wchar_t * pwszName);
    static NameHook * Find(const wchar_t * pwszName);
    static ECode    DestroyAndRelease(NameHook * pNameHook);
    static ECode    Destroy(NameHook * pNameHook);
};

class Object : public SLinkNode
{
public:
    CProcess       *m_pCProcess;
    union
    {
        IStub               *m_pIStub;
        InterfaceStruct     *m_pInterface;
    };
    UInt32          m_uInvokeEntry;
    UInt32          m_uIndex; // default interface index
    oid_t           m_oid;
    EMuid           m_clsid;
    CIClassEntry    *m_pClsInfo;
    NameHook        *m_pNameHook;

public:
    inline void Init(CProcess * pCProcess, IStub * pIStub, UInt32 uIndex,
                        CIClassEntry ** ppClsInfo, NameHook * pNameHook);
};

void Object::Init(CProcess * pCProcess, IStub * pIStub, UInt32 uIndex,
    CIClassEntry ** ppClsInfo, NameHook * pNameHook)
{
    m_oid       = GenerateObjOid();
    m_pCProcess = pCProcess;
    m_pIStub    = pIStub;
    m_uInvokeEntry = ((InterfaceStruct*)pIStub)->m_vPtr-> \
                                            m_vTable[INDEX_OF_INVOKE_IN_VTBL];
    m_uIndex    = uIndex;
    m_pNameHook = pNameHook;
    pIStub->GetClsid(&m_clsid);
    if (ppClsInfo) {
        m_pClsInfo  = *ppClsInfo;
    }
    else {
        m_pClsInfo = NULL;
    }
}

EXTERN_C DECL_CODEINIT void CDECL InitNameHookHash();

#endif //__SVROBJ_H__
